package com.liaogui.consumer.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liaogui.common.entity.userinfo.UserInfoEntity;
import com.liaogui.common.service.userinfo.UserInfoService;
import com.liaogui.common.utils.R;
import com.liaogui.consumer.service.PasswordService;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping("/")
public class RegisterController {
    @Reference
    private UserInfoService userInfoService;
    @Autowired
    private PasswordService passwordService;
    @Autowired
    public static Logger logger = LoggerFactory.getLogger(LoginController.class);
    @RequestMapping(value = "register",method = RequestMethod.POST)
    public R register(HttpServletRequest request){
        UserInfoEntity userInfo = new UserInfoEntity();
        String userName = request.getParameter("NAME");
        String phone = request.getParameter("PHONE");
        String email = request.getParameter("EMAIL");
        String password = request.getParameter("PASSWORD");
        userInfo.setEmail(email);
        userInfo.setPhone(phone);
        userInfo.setPassword(password);
        userInfo.setUserName(userName);
        userInfo.setCreateDate(new Date());
        // 密码加密处理
        passwordService.encryptPassword(userInfo);
        try {
            UserInfoEntity userInfoEntity= userInfoService.selectUserInfo(phone);
            if (null != userInfoEntity){
                return R.error("该手机号码已经注册");
            }
            userInfoService.save(userInfo);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("注冊失败");
            return R.error("注册失败");
        }

    }
}
